package br.ufpe.cin.msc.jrsj2.recommender.ontology;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import br.ufpe.cin.msc.jrsj2.recommender.message.ApplicationMessage;
import br.ufpe.cin.msc.jrsj2.recommender.message.OntologyDuplicatedInstanceMessage;
import br.ufpe.cin.msc.jrsj2.recommender.message.OntologyServiceMessage;
import br.ufpe.cin.msc.jrsj2.recommender.persistence.domain.Channel;
import br.ufpe.cin.msc.jrsj2.recommender.persistence.domain.Program;

public class OntologyServiceHandler {

	private static String responseFormat = ".xml";

	public static void setResponseFormat(String responseFormat) {
		if (!responseFormat.contains("."))
			responseFormat = "." + responseFormat;
		OntologyServiceHandler.responseFormat = responseFormat;
	}

	public static Program findOntologyInstanceForProgram(
			List<Channel> channels, String programTitle) {
		for (Channel channel : channels) {
			String serviceLocation = channel.getServiceLocation();

			try {
				String url = serviceLocation
						+ URLEncoder.encode(programTitle + responseFormat,
								"UTF-8").replace("+", "%20");
				URL serviceURL = new URL(url);
				HttpURLConnection serviceConnection = (HttpURLConnection) serviceURL
						.openConnection();
				serviceConnection.setRequestMethod("GET");

				if (serviceConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
					continue;

				BufferedReader in = new BufferedReader(new InputStreamReader(
						serviceConnection.getInputStream()));
				String ontologyInstance = "";
				String line;

				while ((line = in.readLine()) != null)
					ontologyInstance += line;
				in.close();

				if (ontologyInstance.contains("<programmes>")) {
					String selectedInstance = resolveDuplicatedInstances(ontologyInstance);
					url = serviceLocation
							+ URLEncoder.encode(
									serviceLocation + selectedInstance
											+ responseFormat, "UTF-8").replace(
									"+", "%20");
					serviceURL = new URL(selectedInstance + responseFormat);
					serviceConnection = (HttpURLConnection) serviceURL
							.openConnection();
					in = new BufferedReader(new InputStreamReader(
							serviceConnection.getInputStream()));
					ontologyInstance = "";

					while ((line = in.readLine()) != null)
						ontologyInstance += line;
					in.close();
				}

				ApplicationMessage.setRequestMessage(ontologyInstance);

				OntologyServiceMessage ontologyServiceMessage = new OntologyServiceMessage(
						programTitle);

				ontologyServiceMessage.parseRequestMessage();

				Program program = ontologyServiceMessage.getProgramInstance();
				program.setChannel(channel);

				return program;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static Program getOntologyInstanceForProgram(Channel channel,
			String programTitle) {
		String serviceLocation = channel.getServiceLocation();

		try {
			String url = serviceLocation
					+ URLEncoder.encode(programTitle + responseFormat, "UTF-8");
			url = url.replace("+", "%20");
			URL serviceURL = new URL(url);
			HttpURLConnection serviceConnection = (HttpURLConnection) serviceURL
					.openConnection();
			serviceConnection.setRequestMethod("GET");

			if (serviceConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
				return null;

			BufferedReader in = new BufferedReader(new InputStreamReader(
					serviceConnection.getInputStream()));

			String ontologyInstance = "";
			String line;

			while ((line = in.readLine()) != null)
				ontologyInstance += line;
			in.close();

			if (ontologyInstance.contains("<programmes>")) {
				String selectedInstance = resolveDuplicatedInstances(ontologyInstance);
				serviceURL = new URL(serviceLocation + selectedInstance
						+ responseFormat);

				serviceConnection = (HttpURLConnection) serviceURL
						.openConnection();
				in = new BufferedReader(new InputStreamReader(
						serviceConnection.getInputStream()));

				ontologyInstance = "";

				while ((line = in.readLine()) != null)
					ontologyInstance += line;
				in.close();
			}

			ApplicationMessage.setRequestMessage(ontologyInstance);

			OntologyServiceMessage ontologyServiceMessage = new OntologyServiceMessage(
					programTitle);

			ontologyServiceMessage.parseRequestMessage();

			return ontologyServiceMessage.getProgramInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static String resolveDuplicatedInstances(String ontologyInstances) {
		ApplicationMessage.setRequestMessage(ontologyInstances);

		OntologyDuplicatedInstanceMessage ontologyDuplicatedInstanceMessage = new OntologyDuplicatedInstanceMessage();

		ontologyDuplicatedInstanceMessage.parseRequestMessage();

		return ontologyDuplicatedInstanceMessage.getSelectedInstance();
	}

}
